<?php

namespace App\Admin\Controllers\Security;

use App\Models\CommonRegion;
use App\Models\SecurityCar;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CarController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '安保车辆信息';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SecurityCar());

        $grid->column('id', ___('table.zy_security_car.Id'));
        $grid->column('name', ___('table.zy_security_car.Name'))->editable();
        $grid->column('phone', ___('table.zy_security_car.Phone'))->editable();
        $grid->column('license_plate', ___('table.zy_security_car.License plate'))->editable();
        $grid->column('type', ___('table.zy_security_car.Type'))->radio(SecurityCar::$type);
        $grid->column('province_id', ___('table.zy_security_car.Province id'))->display(function ($id){
            try{
                return CommonRegion::find($id)->name;
            }catch (\Exception $exception){
                return '---';
            }
        });
        $grid->column('city_id', ___('table.zy_security_car.City id'))->display(function ($id){
            try{
                return CommonRegion::find($id)->name;
            }catch (\Exception $exception){
                return '---';
            }
        });
        $grid->column('district_id', ___('table.zy_security_car.District id'))->display(function ($id){
            try{
                return CommonRegion::find($id)->name;
            }catch (\Exception $exception){
                return '---';
            }
        });
        $grid->column('create_time', ___('table.zy_security_car.Create time'));
        $grid->column('update_time', ___('table.zy_security_car.Update time'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SecurityCar::findOrFail($id));

        $show->field('id', ___('table.zy_security_car.Id'));
        $show->field('name', ___('table.zy_security_car.Name'));
        $show->field('phone', ___('table.zy_security_car.Phone'));
        $show->field('license_plate', ___('table.zy_security_car.License plate'));
        $show->field('type', ___('table.zy_security_car.Type'))->as(function ($id){
            return SecurityCar::$type[$id];
        });
        $show->field('province_id', ___('table.zy_security_car.Province id'));
        $show->field('city_id', ___('table.zy_security_car.City id'));
        $show->field('district_id', ___('table.zy_security_car.District id'));
        $show->field('create_time', ___('table.zy_security_car.Create time'));
        $show->field('update_time', ___('table.zy_security_car.Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SecurityCar());

        $form->text('name', ___('table.zy_security_car.Name'));
        $form->mobile('phone', ___('table.zy_security_car.Phone'));
        $form->text('license_plate', ___('table.zy_security_car.License plate'));
        $form->radio('type', ___('table.zy_security_car.Type'))->options(
            SecurityCar::$type
        )->default(1);
        //$form->number('province_id', ___('table.zy_security_car.Province id'));
        $form->select('province_id', ___('table.zy_security_car.Province id'))->options('/admin/ajax/region?q=0')->load('city_id','/admin/ajax/region');
        $form->select('city_id', ___('table.zy_security_car.City id'))->load('district_id','/admin/ajax/region');
        $form->select('district_id', ___('table.zy_security_car.District id'));
        #$form->number('create_time', ___('table.zy_security_car.Create time'));
        #$form->number('update_time', ___('table.zy_security_car.Update time'));

        return $form;
    }


    public function load(Request $request){
        $areaId = $request->get('q');
        $result =  SecurityCar::where('province_id', $areaId)
            ->orWhere('city_id', $areaId)
            ->orWhere('district_id', $areaId)
            ->get(['id', DB::raw('name as text')]);
        return  count($result)?$result:[['id'=>-1, 'text'=>'No Result']];
    }
}
